import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout/index'

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    name: 'DataResource',
    redirect: '/DataResource',
    component: Layout,
    children: [
      {
        path: 'DataResource',
        name: 'DataResource',
        component: () => import('@/views/dataResource/index'),
        meta: { title: '数据资源一张图' }
      }
    ]
  },
  {
    path: '/Emergency',
    name: 'Emergency',
    component: Layout,
    children: [
      {
        path: 'Emergency',
        name: 'Emergency',
        component: () => import('@/views/emergency/index'),
        meta: { title: '应急指挥一张图' }
      }
    ]
  },
  {
    path: '/Database',
    name: 'Database',
    redirect: '',
    component: Layout,
    meta: { title: '数据资源管理' },
    children: [
      {
        path: 'Database',
        name: 'Database',
        component: () => import('@/views/resourceManagement/database'),
        meta: { title: '数据库管理' }
      },
      {
        path: 'DataDirectory',
        name: 'DataDirectory',
        component: () => import('@/views/resourceManagement/dataDirectory'),
        meta: { title: '数据目录管理' }
      }
    ]
  },
  {
    path: '/UserManagement',
    name: 'UserManagement',
    redirect: '',
    component: Layout,
    meta: { title: '数据资源管理' },
    children: [
      {
        path: 'Role',
        name: 'Role',
        component: () => import('@/views/userManagement/role/role'),
        meta: { title: '角色管理' }
      },
      {
        path: 'User',
        name: 'User',
        component: () => import('@/views/userManagement/user/user'),
        meta: { title: '用户管理' }
      },
      {
        path: 'Password',
        name: 'Password',
        component: () => import('@/views/userManagement/password/password'),
        meta: { title: '密码管理' }
      },
      {
        path: 'Logs',
        name: 'Logs',
        component: () => import('@/views/userManagement/logs/logs'),
        meta: { title: '登录日志' }
      },
      {
        path: 'Operlog',
        name: 'Operlog',
        component: () => import('@/views/userManagement/operlog/operlog'),
        meta: { title: '操作日志' }
      },
      {
        path: 'Menu',
        name: 'Menu',
        component: () => import('@/views/userManagement/menu/index'),
        meta: { title: '菜单管理' }
      }
    ]
  },

  {
    path: '/MobileTerminal',
    name: 'MobileTerminal',
    component: () => import('@/views/mobileTerminal'),
    hidden: true
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}
// 解决重复点击导航时，控制台出现报错的问题
const VueRouterPush = Router.prototype.push
Router.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

export default router
